﻿Configuration APMServer
{
    param (
	[Parameter(Mandatory)]
	[ValidateNotNullOrEmpty()]
	[PSObject]$DeploymentTarget
    )

    Import-DscResource -ModuleName cInstaller -Name MESF_cInstallerWrapper
    Import-DscResource -ModuleName cServerFolders -Name MESF_cServerFolders
    Import-DscResource -ModuleName cInstaller -Name MESF_cInstallerWindowsService


    foreach($server in $DeploymentTarget.Servers.Values)
    {
        Node $server.hostName
        {

            #Log Message
            Log "MiddlewareServer_Start_$($server.hostName)" {
                Message = "Start Installation for server $($server.hostName)"
            } 

            #First ensure root folders
            cServerFolders "cServerFolders_$($server.hostName)" {
                    ManagedServer = $server
            }  

            #Ensure server products are installed
            $server.Products | ? {($_.Level -eq 'server') -or ($_.Level -eq 'product')} | Select-Object $Global:DscInstallerWrapper | Foreach-Object {
                cInstallerWrapper "cInstallerWrapper_$($_.UniqueKey)" {
                    Component = $_
                }    
            }

            #Set environment Variables
            $EnvironmentVariables = $server.EnvironmentVariables | Select-Object $Global:DscEnvironmentVariable
            $EnvironmentVariables = Merge-EnvironmentVariables -EnvironmentVariables $EnvironmentVariables
            foreach($environmentVariable in $EnvironmentVariables)
            {
                Log "EnvironmentVariables_$($environmentVariable.uniqueKey)" {
                    Message = "Start EnvironmentVariables for name product $($environmentVariable.uniqueKey)"
                }
                
                Environment "Environment_$($environmentVariable.uniqueKey)" {
                    Ensure = $environmentVariable.Ensure
                    Path = $environmentVariable.Path
                    Name = $environmentVariable.Name
                    Value = $environmentVariable.Value
                }  
            }


            #Copy configuration files on correct folder
            $server.ConfigurationRepositories | Select-Object $DscConfigurationRepositoryObject |  Foreach-Object {
                File "ConfigurationRepositories_$($_.uniqueKey)" {
                    SourcePath=$_.LocalConfigurationPath
                    DestinationPath=$_.Destination
                    Checksum="ModifiedDate"
                    Type=$_.Type
                    Ensure=$_.Ensure
                    Recurse=$true
                    Force=$true
                }
            }

            $server.Products | ? {($_.productName -eq 'Apache')} | Select-Object $global:DscWindowsService | Foreach-Object {
                cInstallerWindowsService "cInstallerWindowsService_$($_.uniqueKey)" {
                    ServiceDefinition=$_
                }
            }


            #Log Message
            Log "MiddlewareServer_End_$($server.hostName)" {
                Message = "End Installation for server $($server.hostName)"
            } 

        }
    }
}